package com.cf_houduan.cf_houduan.controller;

import com.cf_houduan.cf_houduan.config.JwtConfig;
import com.cf_houduan.cf_houduan.entity.UserAccount;
import com.cf_houduan.cf_houduan.service.UserAccountService;
import com.cf_houduan.cf_houduan.util.JsonUtil;
import com.cf_houduan.cf_houduan.util.JwtUtils;
import com.cf_houduan.cf_houduan.util.Result;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user/")
public class UserAccountController {

    @Autowired
    private UserAccountService userAccountService;

    private JwtUtils jwtUtils = new JwtUtils();

    @RequestMapping("/addUser")
    @ResponseBody
    public Result addUser(@RequestBody UserAccount updatePo){
        userAccountService.addPo(updatePo);
        Result result = new Result("");
        return result;
    }

    @RequestMapping("/selectUserList")
    @ResponseBody
    public Result selectUserList(@PathParam("username") String username,@PathParam("curPage") int curPage,@PathParam("curPageSize") int curPageSize){
        UserAccount useraccount = new UserAccount();
        PageInfo<UserAccount> userAccountPageInfo = userAccountService.selectList(useraccount, curPage, curPageSize);
        Result result = new Result(userAccountPageInfo);
        return result;
    }

    @RequestMapping("/selectUserInfo")
    @ResponseBody
    public Result selectUser(@PathParam("username") String username){
        UserAccount useraccount = new UserAccount();
        useraccount.setUsername(username);
        UserAccount userAccount = userAccountService.selectOne(useraccount);
        Result result = new Result(userAccount);
        return result;
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public Result updateUser( @RequestBody UserAccount updatePo){
        //先根据入参查询，查到的数据进行修改，未查到抛出
        UserAccount selectPo = new UserAccount();
        selectPo.setId(updatePo.getId());
        selectPo = userAccountService.selectOne(selectPo);
        //修改
        userAccountService.updatePo(updatePo,selectPo);
        Result result = new Result("");
        return result;
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public Result deleteUser( @RequestBody UserAccount deltePo){
        //先根据入参查询，查到的数据进行删除，未查到抛出
        UserAccount selectPo = userAccountService.selectOne(deltePo);
        //删除
        userAccountService.deletePo(selectPo);
        Result result = new Result("");
        return result;
    }


    @PostMapping("/tokenTest")
    @ResponseBody
    public Result tokenTest(){
        //其他数据以map集合存放在token中
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("companyId", "admin");
        dataMap.put("companyName", "admin");
        String token = jwtUtils.createJwt("1", "2", dataMap);
        return new Result("200", "200", token);
    }



}
